# $1:项目目录 后续参数为$a&$b&$c格式 
# 其中$a为项目子目录，$b为端口号，无则为0000，$c为入口文件,无则为0000
echo "Start deployment"
cd $1
echo "pulling source code..."
git fetch --all
git reset --hard origin/master 
git pull
echo "install..."
for key in "$@"
do
  if [ ${key} != $1 ];
  then
    string=${key}
    array=(${string//,/ })
    cd ${array[0]}
    rm -rf build
    rm -rf node_modules
    npm install
    echo "building..."
    npm run build
    if [ ${array[1]} != '0000' ];
    then
      pid=$(netstat -nlp | grep :${array[1]} | awk '{print $7}' | awk -F"/" '{print $1}');
      if [ -n  "$pid" ]; then
        kill  -9  $pid;
      fi
      nohup node ${array[2]} &
      echo "success."
    fi
  fi
done
echo "finished"
